﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TheOutfield.UmbExt.DocumentSorter
{
    public class DateComparer : IComparer<SimpleDocument>
    {
        #region IComparer<SimpleDocument> Members

        public int Compare(SimpleDocument x, SimpleDocument y)
        {
            if (x == null && y == null)
                return 0; 
            
            if (x == null) 
                return -1; 
            
            if (y == null)
                return 1;

            var xDate = DateTime.Now.AddYears(100);
            DateTime.TryParse(Convert.ToString(x.Field), out xDate);

            var yDate = DateTime.Now.AddYears(100);
            DateTime.TryParse(Convert.ToString(y.Field), out yDate);

            return xDate.CompareTo(yDate);
        }

        #endregion
    }

    public class NumericComparer : IComparer<SimpleDocument>
    {
        #region IComparer<SimpleDocument> Members

        public int Compare(SimpleDocument x, SimpleDocument y)
        {
            if (x == null && y == null)
                return 0;

            if (x == null)
                return -1;

            if (y == null)
                return 1;

            var xNum = (x.Field == null || x.Field.ToString() == string.Empty) ? int.MaxValue : Convert.ToInt32(x.Field);
            var yNum = (y.Field == null || y.Field.ToString() == string.Empty) ? int.MaxValue : Convert.ToInt32(y.Field);

            return xNum.CompareTo(yNum);
        }

        #endregion
    }

    public class AlphaComparer : IComparer<SimpleDocument>
    {
        #region IComparer<SimpleDocument> Members

        public int Compare(SimpleDocument x, SimpleDocument y)
        {
            if (x == null && y == null)
                return 0;

            if (x == null)
                return -1;

            if (y == null)
                return 1;

            var xString = (x.Field == null) ? "zzzzzzzzzz" : Convert.ToString(x.Field);
            var yString = (y.Field == null) ? "zzzzzzzzzz" : Convert.ToString(y.Field);

            return xString.CompareTo(yString);
        }

        #endregion
    }
}